/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.sevenbits.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * 
 * @author vmplanet
 */
public class DBServlet extends HttpServlet {

    /**
     * Handles the HTTP. Sends run to doPost <code>GET</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * Handles the HTTP Parses parameters in input string of request. Do action
     * with database depended by type of request. <code>POST</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String type = request.getParameter("type");
        String req = request.getParameter("request");
        String reqArray[] = {
            type, req
        };
        try {
            WebOut web = new WebOut();
            ArrayList<String> result = web.resp(reqArray);
            request.setAttribute("result", result);
            RequestDispatcher reqDisp = getServletContext()
                .getRequestDispatcher("/index.jsp");
            reqDisp.forward(request, response);
        } finally {
            out.close();

        }

    }

    /**
     * Returns a short description of the servlet.
     * 
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {

        return "Short description";
    }// </editor-fold>
}